package com.pine.sunshineelder.elderheath.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageInfo;
import com.pine.sunshineelder.common.StringHelper;
import com.pine.sunshineelder.common.dao.ElderDao;
import com.pine.sunshineelder.common.dao.ElderHealthDao;
import com.pine.sunshineelder.common.dto.QueryResult;
import com.pine.sunshineelder.common.entity.ElderEntity;
import com.pine.sunshineelder.common.entity.ElderHealthEntity;
import com.pine.sunshineelder.common.exception.ThisSystemException;
import com.pine.sunshineelder.common.validator.ValidateHelper;
import com.pine.sunshineelder.elderheath.ao.ElderHealthAddAo;
import com.pine.sunshineelder.elderheath.service.ElderHealthService;
/**
 * 
 * @author 黄天静
 *
 */
@Service
public class ElderHealthServiceImpl implements ElderHealthService, StringHelper {
	@Autowired
	private ElderHealthDao ehdao;
	@Autowired
	private ElderDao edao;
//	@Autowired
//	private DoctorDao ddao;
	@Override
	public ElderHealthEntity addElderHealth(ElderHealthAddAo ao) throws Exception {
		// 1.验证和清理参数
		String id = uuid();
		String doctorId = ValidateHelper.$("医生的id", ao.getDoctorId());
		String elderId = ValidateHelper.$("老人id", ao.getElderId());
		String bodyCondition = ValidateHelper.$("身体状况", ao.getBodyCondition());
		String elderDite = ValidateHelper.$("老人饮食建议", ao.getElderDiet());
		String content=ValidateHelper.$("检查事项", ao.getContent());
		LocalDateTime checkTime = ao.getCheckTime();
		LocalDateTime createTime = LocalDateTime.now();
		// 2.执行业务逻辑
		ElderEntity e = edao.selectById(elderId);
		// DoctorEntity d=ddao.selectById(doctorId);
//		if (e == null) {
//			throw new ThisSystemException("id.not-exisits", "非法老人id");
//		}
//		if (d == null) {
//			throw new ThisSystemException("id.not-exisits", "非法医生id");
//		}
		// 3.组装结果
		ElderHealthEntity ed = new ElderHealthEntity();
		ed.setId(id);
		ed.setElderId(elderId);
		ed.setDoctorId(doctorId);
		ed.setBodyCondition(bodyCondition);
		ed.setContent(content);
		ed.setElderDiet(elderDite);
		ed.setCheckTime(checkTime);
		ed.setCreateTime(createTime);
		ehdao.insert(ed);
		return ed;
	}

	@Override
	public ElderHealthEntity queryByIdElderHealth(String id) throws Exception {
		// 1.验证和清理参数
		id = ValidateHelper.$("老人健康数据id", id);
		// 2执行业务逻辑
		ElderHealthEntity eh = ehdao.selectById(id);
		if (eh == null) {
			throw new ThisSystemException("id.not-exisits", "非法id");
		}
		return eh;
	}

	@Override
	public QueryResult<ElderHealthEntity> queryBykeyElderHealth(String key, Integer pageNo, Integer pageSize) throws Exception {
		// 1.验证参数
		key = trimAsNull(key);
		// 2.逻辑判断
		if (key != null) {
			// 字符串的拼接，代表前后的数据不限
			key = "%" + key + "%";
		}
		if (pageNo == null) {
			pageNo = 1;
		}
		if (pageSize == null) {
			pageSize = 10;
		}
		// 3.组装逻辑结果
		List<ElderHealthEntity> rows = ehdao.selectByKey(key, pageNo, pageSize);
		PageInfo<ElderHealthEntity> pageInfo = new PageInfo<ElderHealthEntity>(rows);
		int totalRows = (int) pageInfo.getTotal();
		QueryResult result = new QueryResult();
		result.setPageNo(pageNo);
		result.setPageSize(pageSize);
		result.setTotalRows(totalRows);
		result.setRows(rows);
		return result;
	}

}
